﻿using BO.Infrastructure.Utils;
using BugManager;
using ChannelVN.Cached;
using System;
using System.Collections.Generic;
using System.Data;
using Utilities.Entities;


namespace BO.News
{
    public class NewsBO : INewsBO
    {
        private readonly static NewsBO_NoCache NoCacheLayer = new NewsBO_NoCache();

        private static NewsBO _ownerInstance;
        public static INewsBO GetInstance()
        {
            return _ownerInstance ?? (_ownerInstance = new NewsBO());
        }

        static readonly string UniqueText = typeof(NewsBO).Namespace + typeof(NewsBO).Name;

        static string BuildCacheKey(string methodName, IEnumerable<object> listParams)
        {
            return PresentationUtils.GetCacheKey(methodName, listParams, UniqueText);
        }

        #region INewsBO Members
        public DataTable GetNewsByPosition(int top, int typeId, int topExclusion = 0, int zoneId = 0, bool getDataFromMasterDataBase = false)
        {
            var cacheKey = BuildCacheKey("GetNewsByPosition", new object[] { top, typeId, topExclusion, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetNewsByPosition(top, typeId, topExclusion, zoneId, getDataFromMasterDataBase);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public DataTable GetListNews(int pageIndex, int pageSize, int topExclusion = 0, int zoneId = 0)
        {
            var cacheKey = BuildCacheKey("GetListNews", new object[] { pageIndex, pageSize, topExclusion, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetListNews(pageIndex, pageSize, topExclusion, zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(10));
                }
            }
            return result;
        }

        public DataTable GetListNewsWithSubCategory(int pageIndex, int pageSize, int topExclusion = 0, int zoneId = 0)
        {
            var cacheKey = BuildCacheKey("GetListNewsWithSubCategory", new object[] { pageIndex, pageSize, topExclusion, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);

            if (result == null)
            {
                result = NoCacheLayer.GetListNewsWithSubCategory(pageIndex, pageSize, topExclusion, zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(10));
                }
            }
            return result;
        }

        public int GetTotalPagesOfListNews(int pageSize, int topExclusion = 0, int zoneId = 0)
        {
            var cacheName = BuildCacheKey("GetTotalPagesOfListNews", new object[] { pageSize, topExclusion, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<int>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == default(int))
            {
                result = NoCacheLayer.GetTotalPagesOfListNews(pageSize, topExclusion, zoneId);
                CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddHours(1));
            }
            return result;
        }

        public DataTable GetNewsDetail(long newsId)
        {
            var cacheKey = BuildCacheKey("GetNewsDetail", new object[] { newsId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetNewsDetail(newsId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public DataTable GetEmbedNewsOnPage(int top, int zoneId, int type)
        {
            var cacheKey = BuildCacheKey("GetEmbedNewsOnPage", new object[] { top, zoneId, type });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetEmbedNewsOnPage(top, zoneId, type);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(10));
                }
            }
            return result;
        }

        public DataTable GetHotNews(int top, int zoneId)
        {
            var cacheKey = BuildCacheKey("GetHotNews", new object[] { top, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetHotNews(top, zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(10));
                }
            }
            return result;
        }

        public DataTable GetCareableNews(int top)
        {
            var cacheKey = BuildCacheKey("GetCareableNews", new object[] { top });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetCareableNews(top);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(10));
                }
            }
            return result;
        }

        public DataTable GetOlderNews(int top, int zoneId, long newsId, DateTime publishedDate)
        {
            var cacheKey = BuildCacheKey("GetOlderNews", new object[] { top, zoneId, newsId, publishedDate });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetOlderNews(top, zoneId, newsId, publishedDate);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(30));
                }
            }
            return result;
        }

        public CategoryObject GetCategoryDetail(string catUrl)
        {
            var cacheKey = BuildCacheKey("GetCategoryDetail", new object[] { catUrl });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<CategoryObject>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetCategoryDetail(catUrl);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddDays(1));
                }
            }
            return result;
        }

        public CategoryObject GetCategoryDetail(int catId)
        {
            var cacheKey = BuildCacheKey("GetCategoryDetail", new object[] { catId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<CategoryObject>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetCategoryDetail(catId);
                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddDays(1));
                }
            }
            return result;
        }

        public List<CategoryObject> GetAllCategories()
        {
            var cacheKey = BuildCacheKey("GetAllCategories", null);
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<List<CategoryObject>>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetAllCategories();
                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(15));
                }
            }

            return result;
        }

        public TagObject GetTagDetail(int tagId)
        {
            var cacheKey = BuildCacheKey("GetTagDetail", new object[] { tagId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<TagObject>(CacheController.CacheGroupForDataCached, cacheKey);

            if (result == null)
            {
                result = NoCacheLayer.GetTagDetail(tagId);
                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public CategoryObject GetCategoryMetaData(int catId)
        {
            var cacheKey = BuildCacheKey("GetCategoryMetaData", new object[] { catId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<CategoryObject>(CacheController.CacheGroupForDataCached, cacheKey);

            if (result == null)
            {
                result = NoCacheLayer.GetCategoryMetaData(catId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(15));
                }
            }
            return result;
        }

        public OriginalSiteObject GetOriginalSiteDetail(int siteId)
        {
            var cacheKey = BuildCacheKey("GetOriginalSiteDetail", new object[] { siteId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<OriginalSiteObject>(CacheController.CacheGroupForDataCached, cacheKey);

            if (result == null)
            {
                result = NoCacheLayer.GetOriginalSiteDetail(siteId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(30));
                }
            }
            return result;
        }

        public DataTable GetNewsByTag(int tagId, int pageIndex, int pageSize)
        {
            var cacheKey = BuildCacheKey("GetNewsByTag", new object[] { tagId, pageIndex, pageSize });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetNewsByTag(tagId, pageIndex, pageSize);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(10));
                }
            }
            return result;
        }

        public int GetTotalPagesOfListNewsByTag(int tagId, int pageSize)
        {
            var cacheName = BuildCacheKey("GetTotalPagesOfListNewsByTag", new object[] { tagId, pageSize });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<int>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == default(int))
            {
                result = NoCacheLayer.GetTotalPagesOfListNewsByTag(tagId, pageSize);
                CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddHours(1));
            }
            return result;
        }

        public DataTable GetNewsByDate(int day, int month, int year, int pageIndex, int pageSize, int zoneId)
        {
            var cacheKey = BuildCacheKey("GetNewsByDate", new object[] { day, month, year, pageIndex, pageSize, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetNewsByDate(day, month, year, pageIndex, pageSize, zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(10));
                }
            }
            return result;
        }

        public int GetTotalPagesOfListNewsByDate(int day, int month, int year, int pageSize, int zoneId)
        {
            var cacheName = BuildCacheKey("GetTotalPagesOfListNewsByDate", new object[] { day, month, year, pageSize, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<int>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == default(int))
            {
                result = NoCacheLayer.GetTotalPagesOfListNewsByDate(day, month, year, pageSize, zoneId);
                CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddHours(1));
            }
            return result;
        }

        public DataTable GetSportNewsByPosition(int top, int typeId, int topExclusion = 0, int zoneId = 0, bool getDataFromMasterDataBase = false)
        {
            var cacheKey = BuildCacheKey("GetSportNewsByPosition", new object[] { top, typeId, topExclusion, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetSportNewsByPosition(top, typeId, topExclusion, zoneId, getDataFromMasterDataBase);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public DataTable GetMostViewNews(int top, int zoneId)
        {
            var cacheKey = BuildCacheKey("GetMostViewNews", new object[] { top, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetMostViewNews(top, zoneId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(10));
                }
            }
            return result;
        }

        public List<LiveNewsObject> GetLiveNews(long newsId)
        {
            var cacheKey = BuildCacheKey("GetLiveNews", new object[] { newsId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<List<LiveNewsObject>>(CacheController.CacheGroupForDataCached, cacheKey);

            if (result == null)
            {
                result = NoCacheLayer.GetLiveNews(newsId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, result, DateTime.Now.AddMinutes(1));
                }
            }
            return result;
        }

        public DataTable GetEmbedNews(int top, int type, int zoneId = 0)
        {
            var cacheName = CacheKeyManager.EmbedNews(top, type, zoneId);
            var dt = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheName);
            if (dt == null)
            {
                dt = NoCacheLayer.GetEmbedNews(top, type, zoneId);
                if (dt != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, dt, DateTime.Now.AddMinutes(15));
                }
            }

            return dt;
        }

        public NewsObject SEO_GetNewsDetail(long newsId)
        {
            var cacheName = BuildCacheKey("GetNewsSeoMetaData", new object[] { newsId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<NewsObject>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == null)
            {
                result = NoCacheLayer.SEO_GetNewsDetail(newsId);
                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddMinutes(5));
                }
            }
            return result;
        }

        public DataTable GetNewsByType(int top, int type, int zoneId)
        {
            var cacheName = BuildCacheKey("GetNewestPhotoNews", new object[] { top, type, zoneId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == null)
            {
                result = NoCacheLayer.GetNewsByType(top, type, zoneId);
                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public List<EmbedGroup> GetEmbedGroup()
        {
            var cacheName = BuildCacheKey("GetEmbedGroup", null);
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<List<EmbedGroup>>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == null)
            {
                result = NoCacheLayer.GetEmbedGroup();
                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public EmbedThreadGroup GetEmbedThreads(int top, int zoneId, int type)
        {
            var cacheName = BuildCacheKey("GetEmbedThreads", new object[] { top, zoneId, type });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<EmbedThreadGroup>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == null)
            {
                result = NoCacheLayer.GetEmbedThreads(top, zoneId, type);
                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public TagObject GetTagDetail(string tagUrl)
        {
            var cacheName = BuildCacheKey("GetTagDetail", new object[] { tagUrl });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<TagObject>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == null)
            {
                result = NoCacheLayer.GetTagDetail(tagUrl);
                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public DataTable GetBaiGiaoLuuTrucTuyen(int newsId)
        {
            //var cacheName = BuildCacheKey("GetBaiGiaoLuuTrucTuyen", new object[] { newsId });
            //var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheName);
            //if (result == null)
            //{
            var result = NoCacheLayer.GetBaiGiaoLuuTrucTuyen(newsId);
            //    if (result != null)
            //    {
            //        CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddMinutes(3));
            //    }
            //}
            return result;
        }

        public int GetInterviewId(long newsId)
        {
            var cacheName = BuildCacheKey("GetInterviewId", new object[] { newsId });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<int>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == default(int))
            {
                result = NoCacheLayer.GetInterviewId(newsId);
                if (result != default(int))
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        public List<NewsObject> ImportNewsWhileNotEnough(int zoneId, int topHighlight, int topOther)
        {
            var cacheName = BuildCacheKey("ImportNewsWhileNotEnough", new object[] { zoneId, topHighlight, topOther });
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<List<NewsObject>>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == null)
            {
                result = NoCacheLayer.ImportNewsWhileNotEnough(zoneId, topHighlight, topOther);
                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }

        #endregion

        public static List<LinkObject> GetSportSubMenu()
        {
            var cacheKey = BuildCacheKey("GetSportSubMenu", null);
            var result = CacheController.GetCacheDependencyInstance().Get<List<LinkObject>>(cacheKey);
            if (result == null)
            {
                string fileDependency;
                result = NoCacheLayer.GetSportSubMenu(out fileDependency);

                if (result != null && !string.IsNullOrEmpty(fileDependency))
                {
                    CacheController.GetCacheDependencyInstance().Add(fileDependency, cacheKey, result, DateTime.Now.AddDays(1));
                }
            }
            return result;
        }

        public List<CategoryObject> GetAllCategoriesMetaData()
        {
            var cacheKey = BuildCacheKey("GetAllCategoriesMetaData", null);
            var result = CacheController.GetCacheDependencyInstance().Get<List<CategoryObject>>(cacheKey);

            if (result == null)
            {
                string fileDependency;
                result = NoCacheLayer.GetAllCategoriesMetaData(out fileDependency);

                if (result != null)
                {
                    CacheController.GetCacheDependencyInstance().Add(fileDependency, cacheKey, result, DateTime.Now.AddDays(1));
                }
            }
            return result;
        }

        public List<OriginalSiteObject> GetAllOriginalSites()
        {
            var cacheKey = BuildCacheKey("GetAllOriginalSites", null);
            var result = CacheController.GetCacheDependencyInstance().Get<List<OriginalSiteObject>>(cacheKey);

            if (result == null)
            {
                string fileDependency;
                result = NoCacheLayer.GetAllOriginalSites(out fileDependency);

                if (result != null)
                {
                    CacheController.GetCacheDependencyInstance().Add(fileDependency, cacheKey, result, DateTime.Now.AddDays(1));
                }
            }
            return result;
        }

        public static List<Season> GetLichThiDau()
        {
            var cacheName = BuildCacheKey("GetLichThiDau", null);

            var dt = CacheController.GetMemcachedInstanceForDataCached().Get<List<Season>>(CacheController.CacheGroupForDataCached, cacheName);
            if (dt == null)
            {
                dt = NewsBO_NoCache.GetLichThiDau();

                if (dt != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, dt, DateTime.Now.AddMinutes(3));
                }
            }

            return dt;
        }

        public static List<SportRanking> GetBangXepHang()
        {
            string cacheName = BuildCacheKey("GetBangXepHang", null);

            var dt = CacheController.GetMemcachedInstanceForDataCached().Get<List<SportRanking>>(CacheController.CacheGroupForDataCached, cacheName);
            if (dt == null)
            {
                dt = NewsBO_NoCache.GetBangXepHang();
                if (dt != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, dt, DateTime.Now.AddMinutes(3));
                }
            }

            return dt;
        }

        public static bool Vote(int voteId, int[] voteItemIDs)
        {
            try
            {
                NewsBO_NoCache.Vote(voteId, voteItemIDs);
                var cacheName = CacheKeyManager.Vote(voteId);
                CacheController.GetMemcachedInstanceForDataCached().Remove(CacheController.CacheGroupForDataCached, cacheName);
                return true;
            }
            catch (Exception ex)
            {
                ErrorManager.Log("NewsBO", "Vote", ex.ToString());
            }
            return false;

        }

        public static VoteObject GetVote(int voteId)
        {
            var cacheName = CacheKeyManager.Vote(voteId);
            var result = (VoteObject)CacheController.GetMemcachedInstanceForDataCached().Get(CacheController.CacheGroupForDataCached, cacheName);
            if (result == null)
            {
                result = NewsBO_NoCache.GetVote(voteId);

                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddDays(1));
                }
            }

            return result;


        }

        #region News MetaData
        public static NewsObject GetNewsMetaData(long newsId)
        {
            var cacheKey = CacheKeyManager.NewsMetaData(newsId);
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<NewsObject>(CacheController.CacheGroupForDataCached, cacheKey);
            if (result == null)
            {
                result = NoCacheLayer.GetNewsMetaData(newsId);
                if (result != null)
                    AddNewsMetaData(result);
            }
            return result;
        }

        public static void AddNewsMetaData(NewsObject objNews)
        {
            if (objNews == null) return;
            var cacheKey = CacheKeyManager.NewsMetaData(objNews.NewsId);
            CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheKey, objNews, DateTime.Now.AddDays(1));
        }
        #endregion

        #region Tag MetaData
        public static TagObject GetTagMetaData(string tagUrl)
        {
            var cacheKey = CacheKeyManager.TagMetaData(tagUrl);
            return CacheController.GetMemcachedInstanceForTag().Get<TagObject>(CacheController.CacheGroupForDataCached, cacheKey);
        }

        public static void AddTagMetaData(TagObject objTag)
        {
            var cacheKey = CacheKeyManager.TagMetaData(objTag.DisplayUrl);
            CacheController.GetMemcachedInstanceForTag().Add(CacheController.CacheGroupForDataCached, cacheKey, objTag, DateTime.Now.AddMonths(1));
        }
        #endregion

        public class CacheKeyManager
        {
            public static string EmbedNews(int top, int type, int zoneId = 0)
            {
                return BuildCacheKey("EmbedNews", new object[] { top, type, zoneId });
            }

            public static string TagMetaData(string tagUrl)
            {
                return BuildCacheKey("TagMetaData", new object[] { tagUrl });
            }

            public static string NewsMetaData(long newsId)
            {
                return BuildCacheKey("NewsMetaData", new object[] { newsId });
            }

            public static string Vote(int voteId)
            {
                return BuildCacheKey("GetVote", new object[] { voteId });
            }
        }
    }
}
